<?php

require_once("page.class.php");

class kampanja extends page{
	
	function kampanja(){
		$this->db = new db_functions();
		
		$a = getFromInput("a");
		$mod = getFromInput("mod");
		
		switch ($a){
			case "delete":
				$this->kampanjeDelete();
				break;
			case "new":
				$this->kampanjeNew();
				break;
			default:
				$this->kampanjeBrowse();
				break;
		}
	}
	
	function kampanjeBrowse(){
		$polja = $this->get_Kampanje();
		foreach ($polja["polje_start"] as $k=>$v){
			$polja["polje_start"][$k] = int2date($v);
		}
		foreach ($polja["polje_end"] as $k=>$v){
			$polja["polje_end"][$k] = int2date($v);
		}
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("kampanja_start",$polja["polje_start"]);
			$this->assign("kampanja_end",$polja["polje_end"]);
		echo $this->parseTemplate("kampanje/browse.tpl");
	}
	
	function kampanjeNew(){
		$code = 0;
		$msg = "Kampanja je sačuvana.";
		
		$id = getFromInput("edit_kampanja_id",true);
		$name = readValFromInput("edit_kampanja_name");
		$start = date2int(getFromInput("edit_kampanja_start"));
		$end = date2int(getFromInput("edit_kampanja_end"));
		
		if (strlen($id)) {
			$q = "UPDATE kampanje SET "
				."kampanja_naziv='".prepForSQL($name)."', "
				."kampanja_start_date='$start', "
				."kampanja_end_date='$end' "
				."WHERE kampanja_id=$id LIMIT 1";
		}else{
			$q = "INSERT INTO kampanje(kampanja_naziv, kampanja_start_date, kampanja_end_date) "
				."VALUES('".prepForSQL($name)."','$start','$end')";
		}
		
		if (!$this->db->alterTable($q)) {
			$code = 1;
			$msg = mysql_error();
		}
		
		echo "$code{!}$msg";
	}
	
	function kampanjeDelete(){
		$code = 0;
		$msg = "Kampanja je obrisana";
		
		$id = getFromInput("id");
		
		$q = "DELETE FROM kampanje WHERE kampanja_id=$id LIMIT 1";
		if (!$this->db->alterTable($q)) {
			$code = 1;
			$msg = mysql_error();
		}
		
		echo "$code{!}$msg";
	}
}

?>